package com.dangdang.action.authorize;

import com.dangdang.action.BaseAction;
import com.dangdang.domain.User;
import com.dangdang.service.AuthorizeService;
import com.dangdang.service.ServiceFactory;
import com.dangdang.util.Constants;

/**
 * 用户登录控制层
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-1-26
 */
public class LoginAction extends BaseAction {

	/** 用登录请求时所在的页面 */
	private String uri;

	/** 用户登录邮箱 */
	private String email;

	/** 用户登录密码 */
	private String password;

	private final AuthorizeService us = ServiceFactory.getAuthorizeService();

	public String getPassword() {
		return password;
	}

	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUri() {
		return this.uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String execute() throws Exception {
		User user = us.validateUser(email, password);
		if (user != null) {
			session.put(Constants.USER_KEY, user);
			if (user.isVerify()) {// 已经验证通过的用户,转到刚刚来的页面
				if (null == uri) {// 如果uri是空,则说是直接请求登录界面的,这样让他Redirect到主页面
					return "none";
				} else {
					return "success";
				}
			} else {
				return "verify"; // 没通过验证 的家伙，让他们都去验证自己
			}
		} else {
			// 用户名或者密码输入不正确,给他个提示,该提示在session里面
			put("msg", "用户名密码不正确");
			return "input";
		}
	}
}
